package com.yunxi.estate.config.core.controller;

import com.yunxi.estate.config.core.domain.AjaxResult;
import com.yunxi.estate.config.core.domain.LoginBody;
import com.yunxi.estate.config.core.domain.RegisterBody;
import com.yunxi.estate.config.core.service.ILoginService;
import com.yunxi.estate.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * 通用请求处理
 *
 * @author 松子鱼
 */
@Controller
public class CommonController {

    @Autowired
    ILoginService loginService;

    int verifySum = 0;
    double resultSum = 0.0;

    /**
     * 登录方法
     *
     * @param request  请求值
     * @param response 响应值
     * @return 跳转界面
     */
    @RequestMapping({"/", "/login"})
    public String login(HttpServletRequest request, HttpServletResponse response, SysUser user) {
        HttpSession httpSession = request.getSession();
        LoginBody loginInfo = new LoginBody();

        SysUser cookiesUser = loginService.CookiesCheck(request, user);

        if (!Objects.equals(cookiesUser.getUserId(), "0") && cookiesUser.getPassword() != null) {
            loginInfo.setPhone(cookiesUser.getPhone());
            loginInfo.setPassword(cookiesUser.getPassword());
            SysUser loginUser = loginService.selectLoginUser(loginInfo);
            loginService.insertCookies(httpSession, loginUser, response);
        }

        SysUser checkUser = (SysUser) httpSession.getAttribute("user");

        if (checkUser != null) {
            return "redirect:/index";
        } else {
            return "login";
        }
    }

    /**
     * 执行登录
     *
     * @param login       登录信息
     * @param httpSession 登录会话
     * @param response    响应值
     * @return 登录结果状态
     */
    @PostMapping("/doLogin")
    @ResponseBody
    public AjaxResult doLogin(LoginBody login, HttpSession httpSession, HttpServletResponse response) {
        SysUser loginUser = loginService.selectLoginUser(login);
        return loginService.login(loginUser, httpSession, response);
    }

    /**
     * 注册方法
     *
     * @param register 注册对象
     * @param session  会话
     * @return 跳转界面
     */
    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(RegisterBody register, HttpSession session) {
        return loginService.register(register, session);
    }

    /**
     * 不同权限首页跳转
     *
     * @param session 会话
     * @return 跳转界面
     */
    @RequestMapping("/index")
    public String index(HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        // 对应权限进入不同页面
        if (user != null) {
            if (user.getRoleId() == 1) {
                return "redirect:/admin/index";
            } else if (user.getRoleId() == 2) {
                return "redirect:/resident/index";
            } else {
                return "redirect:/rider/index";
            }
        } else {
            return "login";
        }
    }

    /**
     * 登出
     *
     * @param session  会话
     * @param response 响应
     * @return 跳转界面
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session, HttpServletResponse response) {
        Cookie phone = new Cookie("phone", "");
        phone.setMaxAge(0);
        phone.setHttpOnly(true);
        Cookie password = new Cookie("password", "");
        password.setMaxAge(0);
        password.setHttpOnly(true);
        response.addCookie(phone);
        response.addCookie(password);
        session.removeAttribute("user");
        return "redirect:/login";
    }

    /**
     * 验证码校验
     *
     * @param verifyData 校验数据集
     * @return 校验结果
     */
    @PostMapping("/captcha")
    @ResponseBody
    public boolean captchaVerify(@RequestBody List<Integer> verifyData) {
        for (Integer data : verifyData) {
            verifySum += data;
        }
        double avg = verifySum * 1.0 / verifyData.size();
        for (Integer data : verifyData) {
            resultSum += Math.pow(data - avg, 2);
        }
        double result = resultSum / verifyData.size();
        return result != 0;
    }
}
